//
//  CityTableViewCell.m
//  MySelectCityDemo
//
//  Created by ZJ on 15/10/28.
//  Copyright © 2015年 WXDL. All rights reserved.
//

#import "CityTableViewCell.h"
#import "UIView+Extension.h"
#define  ScreenWidth [UIScreen mainScreen].bounds.size.width
@implementation CityTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cityArray:(NSArray*)array
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _cityArray =array;
        self.backgroundColor = Gray_Color;
        int count = 0;
        float btnWidth = 0;
        float viewHeight = 0;
        for (int i = 0; i < _cityArray.count; i++) {
            
            NSString *btnName = _cityArray[i];
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setBackgroundColor:[UIColor whiteColor]];
            [btn setTitleColor:TextColor3 forState:UIControlStateNormal];
            btn.tag=i;
            btn.titleLabel.font = FontAndStyle12;
            [btn setTitle:btnName forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

            CGSize btnSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font }];
            
            btn.width = btnSize.width + 30;
            btn.height = btnSize.height + 10;
            
            if (i==0)
            {
                btn.x = 10;
                btnWidth += CGRectGetMaxX(btn.frame);
            }
            else{
                btnWidth += CGRectGetMaxX(btn.frame)+20;
                if (btnWidth > MainW-25) {
                    count++;
                    btn.x = 10;
                    btnWidth = CGRectGetMaxX(btn.frame);
                }
                else{
                    
                    btn.x += btnWidth - btn.bsg_width;
                }
            }
            btn.y += count * (btn.bsg_height+10)+10;
            
            viewHeight = CGRectGetMaxY(btn.frame)+10;
            ViewRadius(btn, 3);
            [self addSubview:btn];
            
            self.height=viewHeight+15;
        }
    }
    return self;
}
-(void)click:(UIButton *)btn
{
    if(_cityArray.count==1&btn.tag==0)
    {
        self.didSelectedBtn(1111);
    }
    else
    {
        self.didSelectedBtn((int)btn.tag);
    }
}
@end
